package com.weixin.project.Common;

import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Common.Exception.*;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(CurrentUserNullException.class)
    public ResponseEntity<JSONObject> handleCurrentUserNullException(CurrentUserNullException ex) {
        return new ResponseEntity<>(ResponseUtil.userNotFind(), HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(EntityNotExistException.class)
    public ResponseEntity<JSONObject> handleEntityNotExistException(EntityNotExistException ex) {
        return new ResponseEntity<>(ResponseUtil.error(ex.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(JSONParseException.class)
    public ResponseEntity<JSONObject> handleJSONParseException(JSONParseException ex) {
        return new ResponseEntity<>(ResponseUtil.error(ex.getMessage()), HttpStatus.OK);
    }

    @ExceptionHandler(JSONSchemaNotMatchException.class)
    public ResponseEntity<JSONObject> handleJSONSchemaNotMatchException(JSONSchemaNotMatchException ex) {
        return new ResponseEntity<>(ResponseUtil.error(ex.getMessage()), HttpStatus.OK);
    }

    @ExceptionHandler(JSONFieldIncorrectException.class)
    public ResponseEntity<JSONObject> handleJSONFieldIncorrectException(JSONFieldIncorrectException ex) {
        return new ResponseEntity<>(ResponseUtil.error(ex.getMessage()), HttpStatus.OK);
    }

    @ExceptionHandler(FileNotValidException.class)
    public ResponseEntity<JSONObject> handleFileNotValidException(FileNotValidException ex) {
        return new ResponseEntity<>(ResponseUtil.error(ex.getMessage()), HttpStatus.OK);
    }
}
